package org.to_zero.ssm.sdm.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.to_zero.ssm.sdm.entity.Team;
import org.to_zero.ssm.sdm.entity.User;
import org.to_zero.ssm.sdm.entity.vo.ResponseEntity;
import org.to_zero.ssm.sdm.mapper.TeamMapper;
import org.to_zero.ssm.sdm.mapper.UserMapper;
import org.to_zero.ssm.sdm.service.TeamService;
import org.to_zero.ssm.sdm.util.PinginUtil;
import org.to_zero.ssm.sdm.util.Toolkit;

import java.util.*;

@Service
public class TeamServiceImpl implements TeamService {

    @Autowired
    private TeamMapper teamMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public ResponseEntity handleTeamList() {
        Map<Integer, String> selectMap = new LinkedHashMap<>();
        List<Team> teamList = teamMapper.selectMap();
        for (Team team : teamList) {
            selectMap.put(team.getId(), team.getTeamName());
        }
        return selectMap.isEmpty() ? ResponseEntity.error() : ResponseEntity.success(selectMap);
    }

    @Override
    public ResponseEntity findTeamByCondition(Team team) {
        return ResponseEntity.success(teamMapper.findTeamByCondition(team));
    }

    @Override
    @Transactional
    public ResponseEntity update(Team team) {
        int flg = 0;
        if (Toolkit.isNull(team.getId())) {
            String teamCode = PinginUtil.toPinyin(team.getTeamName());
            team.setTeamCode(teamCode);
            flg = teamMapper.insertSelective(team);
        } else {
            flg = teamMapper.updateByPrimaryKeySelective(team);
            Integer teamLeaderId = team.getTeamLeaderId();
            if (Objects.nonNull(teamLeaderId)) {
                User user = User.builder().id(teamLeaderId).roleCode("leader").build();
                userMapper.updateByPrimaryKeySelective(user);
            }
        }
        return flg > 0 ? ResponseEntity.success("更新成功") : ResponseEntity.error("出了一点问题o(╥﹏╥)o");
    }

    @Override
    @Transactional
    public ResponseEntity deleteById(Integer id) {
        clearTeamMember(id);
        int flg = teamMapper.deleteByPrimaryKey(id);
        return flg > 0 ? ResponseEntity.success("删除成功") : ResponseEntity.error("出了一点问题o(╥﹏╥)o");
    }

    @Override
    public ResponseEntity clearTeamMember(Integer id) {
        return teamMapper.clearTeamMember(id) > 0 ? ResponseEntity.success() : ResponseEntity.error();
    }

    @Override
    public ResponseEntity deleteByIds(Integer[] ids) {
        int flg = teamMapper.deleteByPrimaryKeys(ids);
        return flg > 0 ? ResponseEntity.success("批量删除成功") : ResponseEntity.error("出了一点问题o(╥﹏╥)o");
    }

    @Override
    public ResponseEntity teamLeaderMap(Integer id) {
        Map<Integer, String> teamLeaderMap = new LinkedHashMap<>();
        User user = User.builder().teamId(id).build();
        List<User> memberList = userMapper.findUserByCondition(user);
        for (User member : memberList) {
            teamLeaderMap.put(member.getId(), member.getName());
        }
        return teamLeaderMap.isEmpty() ? ResponseEntity.error() : ResponseEntity.success(teamLeaderMap);
    }

    @Override
    public ResponseEntity checkMember(Integer id) {
        User user = User.builder().teamId(id).build();
        return ResponseEntity.success(userMapper.findUserByCondition(user));
    }

    @Override
    public ResponseEntity unAllocatedAndMemberList(Integer id) {
        return ResponseEntity.success(teamMapper.allotTeamMember(id));
    }

    @Override
    @Transactional
    public ResponseEntity allotMember(Integer[] ids, Team team) {
        User cond = User.builder().teamId(team.getId()).build();
        Integer teamId = team.getId(), teamLeaderId = team.getTeamLeaderId();
        List<User> teamMemberList = userMapper.findUserByCondition(cond);
        List<Integer> rejectMemberIds = new ArrayList<>();
        List<Integer> insertMemberIds = new ArrayList<>();
        for (User user : teamMemberList) {
            boolean isReject = true;
            for (Integer id : ids) {
                if (user.getId().equals(id)) {
                    isReject = false;
                    break;
                }
            }
            if (isReject) {
                //排除id为当前组长时,置空小组组长
                int rejectId = user.getId();
                if (Objects.nonNull(teamLeaderId) && teamLeaderId.equals(rejectId)) {
                    team.setTeamLeaderId(null);
                    teamMapper.updateByPrimaryKeySelective(team);
                }
                rejectMemberIds.add(user.getId());
            }
        }
        for (Integer id : ids) {
            boolean isInsert = true;
            for (User user : teamMemberList) {
                if (user.getId().equals(id)) {
                    isInsert = false;
                    break;
                }
            }
            if (isInsert) insertMemberIds.add(id);
        }
        try {
            if (rejectMemberIds.size() > 0) teamMapper.rejectMembers(rejectMemberIds);
            if (insertMemberIds.size() > 0) teamMapper.insertMembers(insertMemberIds, teamId);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.error("出了一点问题o(╥﹏╥)o");
        }
        return ResponseEntity.success("操作成功");
    }
}
